
package com.spring.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 功能描述,该部分必须以中文句号结尾。
 *
 * @author panqingcui
 * @create 2017-02-20 15:34
 */
@RestController
public class ServiceController {
    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private DiscoveryClient client;

    @RequestMapping(value = "/add" ,method = RequestMethod.GET)
    public Integer add(@RequestParam Integer a, @RequestParam Integer b) {
        List<ServiceInstance> instance = client.getInstances("spring-cloud-service-server");
        Integer r = a + b;
        for(ServiceInstance s:instance){
            logger.info("host:"+s.getHost());
            logger.info("port:"+s.getPort());
            logger.info("url:"+s.getUri());
        }
        return r;
    }
    @RequestMapping("/")
    public String greeting() {
        return "Hello World!";
    }

}
